package com.hmkj.admin.controller.amount;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.entity.export.ExportUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.account.AccountCashModel;
import com.hmkj.core.model.account.CashModel;
import com.hmkj.core.po.account.AccountCash;
import com.hmkj.core.service.account.AccountCashService;
import com.hmkj.core.service.user.UserService;
import com.xiaoleilu.hutool.util.StrUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class AccountCashJsonController extends GenericController<Integer, AccountCash> {

    @Autowired
    private AccountCashService accountCashService;

    /**
     * 提现管理列表数据
     *
     * @param dtGridPager
     * @return
     */
    @RequestMapping(value = "/cash/list.json", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("sys:cash:list")
    public DtGrid listJson(String dtGridPager) throws Exception {
        dtGridPager = dataAuthority(dtGridPager);
        return accountCashService.getDtGridList(dtGridPager, AccountCashModel.class);
    }

    /**
     * 提現（人工）
     */
    @RequestMapping(value = "/cash/save.json", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("sys:cash:save")
    public ResultEntity recharge(AccountCash accountCash) throws Exception {
        accountCashService.addCashMoney(accountCash);
        return ok();
    }

    /**
     * 提现审核
     *
     * @param cashModel 提现记录对象
     * @return ResultEntity
     * @throws Exception ex
     */
    @ResponseBody
    @RequestMapping("cash/json/audit")
    @RequiresPermissions("sys:cash:audit")
    public ResultEntity doAudit(CashModel cashModel) throws Exception {
        if (null == cashModel || cashModel.getId() == null || cashModel.getId() <= 0) {
            throw new BussinessException("参数错误");
        }
        //获取备注内容的长度
        if (StrUtil.isBlank(cashModel.getRemark())) {
            throw new BussinessException("【审核备注】不能为空");
        }
        int len = 255;
        if (cashModel.getRemark().length() > len) {
            throw new BussinessException("【审核备注】输入的内容长度超过容量，请缩短输入的内容");
        }
        //状态校验
        boolean b = cashModel.getAuditStatus() != CashModel.AUDITSTATUS.T1.code && cashModel.getAuditStatus() != CashModel.AUDITSTATUS.T2.code;
        if (null == cashModel.getAuditStatus() || b) {
            throw new BussinessException("审核状态有误");
        }
        if (cashModel.getChannel() <= 0) {
            throw new BussinessException("请选择代付通道");
        }

       if (CashModel.CHANNEL.T1.code == cashModel.getChannel()) {
            // 连连代付提现
            accountCashService.doAuditLianLian(cashModel);
        } else {
            throw new BussinessException("代付通道不存在");
        }
        return ok();
    }

    /**
     * 列表导出
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "cash/export.json", method = RequestMethod.POST)
    public ResultEntity export(String dtGridPager, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 执行导出
        ExportUtils.export(request, response, accountCashService.getDtGridListExport(dtGridPager, AccountCashModel.class));
        return ok();
    }
}
